home *** CD-ROM | disk | FTP | other *** search
/ Amiga Plus 1995 #5 & #6 / Amiga Plus CD - 1995 - No. 5 and 6.iso / pd / netz / term / extras / source / term-source.lha / termScale.c < prev    next >
C/C++ Source or Header  |  1995-07-01  |  8KB  |  411 lines

  1. /*
  2. **    termScale.c
  3. **
  4. **    Single scaled character output routines
  5. **
  6. **    Copyright © 1990-1995 by Olaf `Olsen' Barthel
  7. **        All Rights Reserved
  8. */
  9.  
  10. #include "termGlobal.h"
  11.  
  12.     /* Some static data required by the bitmap scaling routine. */
  13.  
  14. STATIC struct RastPort        *ScaleRPort;
  15. STATIC struct BitMap        *ScaleSrcBitMap,
  16.                 *ScaleDstBitMap;
  17. STATIC struct BitScaleArgs    *ScaleArgs;
  18.  
  19. STATIC WORD             ScaleCache    = -1,
  20.                  ScaleType    = 42,
  21.                  ScaleConfig    = 42;
  22.  
  23.     /* DeleteScale():
  24.      *
  25.      *    Frees all the data associated with font scaling.
  26.      */
  27.  
  28. VOID
  29. DeleteScale()
  30. {
  31.     if(ScaleArgs)
  32.     {
  33.         FreeVecPooled(ScaleArgs);
  34.  
  35.         ScaleArgs = NULL;
  36.     }
  37.  
  38.     if(ScaleDstBitMap)
  39.     {
  40.         DeleteBitMap(ScaleDstBitMap);
  41.  
  42.         ScaleDstBitMap = NULL;
  43.     }
  44.  
  45.     if(ScaleSrcBitMap)
  46.     {
  47.         DeleteBitMap(ScaleSrcBitMap);
  48.  
  49.         ScaleSrcBitMap = NULL;
  50.     }
  51.  
  52.     if(ScaleRPort)
  53.     {
  54.         FreeVecPooled(ScaleRPort);
  55.  
  56.         ScaleRPort = NULL;
  57.     }
  58. }
  59.  
  60.     /* CreateScale():
  61.      *
  62.      *    Sets up the data required for real-time font scaling
  63.      *    (bitmaps, rastports, etc.).
  64.      */
  65.  
  66. BYTE __regargs
  67. CreateScale(struct Window *Parent)
  68. {
  69.         /* Create a RastPort to render into. */
  70.  
  71.     if(ScaleRPort = (struct RastPort *)AllocVecPooled(sizeof(struct RastPort),MEMF_ANY | MEMF_CLEAR))
  72.     {
  73.         WORD    MaxWidth,
  74.             PlaneWidth,
  75.             PlaneHeight;
  76.         LONG    Depth;
  77.  
  78.             /* Initialize it. */
  79.  
  80.         InitRastPort(ScaleRPort);
  81.  
  82.         if(GFX)
  83.             MaxWidth = GFX -> tf_XSize;
  84.         else
  85.             MaxWidth = 0;
  86.  
  87.         if(TextFontWidth > MaxWidth)
  88.             MaxWidth = TextFontWidth;
  89.  
  90.             /* Remember dimensions. */
  91.  
  92.         PlaneWidth    = MaxWidth;
  93.         PlaneHeight    = TextFontHeight;
  94.  
  95.             /* Check the depth of the original screen. */
  96.  
  97.         Depth = GetBitMapDepth(Parent -> RPort -> BitMap);
  98.  
  99.             /* Create the bitmap to render into. */
  100.  
  101.         if(ScaleSrcBitMap = (struct BitMap *)CreateBitMap(PlaneWidth,PlaneHeight,Depth,BMF_CLEAR,Parent -> RPort -> BitMap))
  102.         {
  103.                 /* Create the bitmap to place the scaled font data into. */
  104.  
  105.             if(ScaleDstBitMap = (struct BitMap *)CreateBitMap(PlaneWidth * 2,PlaneHeight * 2,Depth,BMF_CLEAR,Parent -> RPort -> BitMap))
  106.             {
  107.                     /* Put the source bitmap into the source RastPort. */
  108.  
  109.                 ScaleRPort -> BitMap = ScaleSrcBitMap;
  110.  
  111.                     /* Install the fonts. */
  112.  
  113.                 SetFont(ScaleRPort,CurrentFont);
  114.  
  115.                     /* Set the default rendering pens. */
  116.  
  117.                 SetAPen(ScaleRPort,1);
  118.                 SetBPen(ScaleRPort,0);
  119.  
  120.                     /* By default, overwrite data. */
  121.  
  122.                 SetDrMd(ScaleRPort,JAM2);
  123.  
  124.                     /* Allocate space for the bitmap scaling arguments. */
  125.  
  126.                 if(ScaleArgs = (struct BitScaleArgs *)AllocVecPooled(sizeof(struct BitScaleArgs),MEMF_ANY | MEMF_CLEAR))
  127.                 {
  128.                         /* Initialize the structure. */
  129.  
  130.                     ScaleArgs -> bsa_SrcWidth    = TextFontWidth;
  131.                     ScaleArgs -> bsa_SrcHeight    = TextFontHeight;
  132.  
  133.                     ScaleArgs -> bsa_YSrcFactor    = 1;
  134.  
  135.                     ScaleArgs -> bsa_SrcBitMap    = ScaleSrcBitMap;
  136.                     ScaleArgs -> bsa_DestBitMap    = ScaleDstBitMap;
  137.  
  138.                     return(TRUE);
  139.                 }
  140.             }
  141.         }
  142.     }
  143.  
  144.     return(FALSE);
  145. }
  146.  
  147.     /* PrintScaled(UBYTE Char,UBYTE Scale):
  148.      *
  149.      *    This is the big one: since VT100 supports a number of
  150.      *    font sizes (double height, double width, 132 columns),
  151.      *    the approriate characters are scaled in real-time before
  152.      *    they are displayed.
  153.      */
  154.  
  155. VOID __regargs
  156. PrintScaled(STRPTR Buffer,LONG Size,UBYTE Scale)
  157. {
  158.     UWORD SrcY,DestX,DestY,SizeX,Baseline;
  159.  
  160.         /* Determine the scale of the destination character. */
  161.  
  162.     if(CurrentCharWidth == SCALE_HALF)
  163.     {
  164.             /* Determine scale to be used. */
  165.  
  166.         switch(Scale)
  167.         {
  168.                 /* Half width. */
  169.  
  170.             case SCALE_ATTR_NORMAL:
  171.  
  172.                 ScaleArgs -> bsa_XDestFactor    = 1;
  173.                 ScaleArgs -> bsa_YDestFactor    = 1;
  174.                 ScaleArgs -> bsa_XSrcFactor    = 2;
  175.  
  176.                 SrcY    = 0;
  177.                 DestX    = MUL_X(CursorX) / 2;
  178.                 SizeX    = TextFontWidth / 2;
  179.  
  180.                 ScaleCache = -1;
  181.  
  182.                 break;
  183.  
  184.                 /* Half width, double height (top bits). */
  185.  
  186.             case SCALE_ATTR_TOP2X:
  187.  
  188.                 ScaleArgs -> bsa_XDestFactor    = 1;
  189.                 ScaleArgs -> bsa_YDestFactor    = 2;
  190.                 ScaleArgs -> bsa_XSrcFactor    = 1;
  191.  
  192.                 SrcY    = 0;
  193.                 DestX    = MUL_X(CursorX);
  194.                 SizeX    = TextFontWidth;
  195.  
  196.                 ScaleCache = -1;
  197.  
  198.                 break;
  199.  
  200.                 /* Half width, double height (bottom bits). */
  201.  
  202.             case SCALE_ATTR_BOT2X:
  203.  
  204.                 ScaleArgs -> bsa_XDestFactor    = 1;
  205.                 ScaleArgs -> bsa_YDestFactor    = 2;
  206.                 ScaleArgs -> bsa_XSrcFactor    = 1;
  207.  
  208.                 SrcY    = TextFontHeight;
  209.                 DestX    = MUL_X(CursorX);
  210.                 SizeX    = TextFontWidth;
  211.  
  212.                 ScaleCache = -1;
  213.  
  214.                 break;
  215.         }
  216.     }
  217.     else
  218.     {
  219.             /* Determine scale to be used. */
  220.  
  221.         switch(Scale)
  222.         {
  223.                 /* Double height (top bits). */
  224.  
  225.             case SCALE_ATTR_TOP2X:
  226.  
  227.                 ScaleArgs -> bsa_XDestFactor    = 2;
  228.                 ScaleArgs -> bsa_YDestFactor    = 2;
  229.                 ScaleArgs -> bsa_XSrcFactor    = 1;
  230.  
  231.                 SrcY    = 0;
  232.                 DestX    = MUL_X(CursorX) * 2;
  233.                 SizeX    = TextFontWidth * 2;
  234.  
  235.                 ScaleCache = -1;
  236.  
  237.                 break;
  238.  
  239.                 /* Double height (bottom bits). */
  240.  
  241.             case SCALE_ATTR_BOT2X:
  242.  
  243.                 ScaleArgs -> bsa_XDestFactor    = 2;
  244.                 ScaleArgs -> bsa_YDestFactor    = 2;
  245.                 ScaleArgs -> bsa_XSrcFactor    = 1;
  246.  
  247.                 SrcY    = TextFontHeight;
  248.                 DestX    = MUL_X(CursorX) * 2;
  249.                 SizeX    = TextFontWidth * 2;
  250.  
  251.                 ScaleCache = -1;
  252.  
  253.                 break;
  254.  
  255.                 /* Double width. */
  256.  
  257.             case SCALE_ATTR_2X:
  258.  
  259.                 ScaleArgs -> bsa_XDestFactor    = 2;
  260.                 ScaleArgs -> bsa_YDestFactor    = 1;
  261.                 ScaleArgs -> bsa_XSrcFactor    = 1;
  262.  
  263.                 SrcY    = 0;
  264.                 DestX    = MUL_X(CursorX) * 2;
  265.                 SizeX    = TextFontWidth * 2;
  266.  
  267.                 ScaleCache = -1;
  268.  
  269.                 break;
  270.         }
  271.     }
  272.  
  273.     ScaleType    = Scale;
  274.     ScaleConfig    = CurrentCharWidth;
  275.  
  276.         /* Look for the font type to scale. */
  277.  
  278.     if(ScaleRPort -> Font != CurrentFont)
  279.     {
  280.         SetFont(ScaleRPort,CurrentFont);
  281.  
  282.         ScaleArgs -> bsa_SrcWidth = TextFontWidth;
  283.  
  284.         ScaleCache = -1;
  285.     }
  286.  
  287.         /* Set the approriate colours. */
  288.  
  289.     if(ReadAPen(ScaleRPort) != ReadAPen(RPort))
  290.     {
  291.         SetAPen(ScaleRPort,ReadAPen(RPort));
  292.  
  293.         ScaleCache = -1;
  294.     }
  295.  
  296.     if(ReadBPen(ScaleRPort) != ReadBPen(RPort))
  297.     {
  298.         SetBPen(ScaleRPort,ReadBPen(RPort));
  299.  
  300.         ScaleCache = -1;
  301.     }
  302.  
  303.         /* Calculate topmost line to write to. */
  304.  
  305.     DestY = MUL_Y(CursorY);
  306.  
  307.         /* Remember the font baseline. */
  308.  
  309.     Baseline = CurrentFont -> tf_Baseline;
  310.  
  311.     if(CurrentFont == GFX)
  312.     {
  313.         BYTE Mode = 1;
  314.  
  315.             /* Run down the buffer... */
  316.  
  317.         while(Size--)
  318.         {
  319.             if(GfxTable[*Buffer] == Mode)
  320.             {
  321.                 if(*Buffer != ScaleCache)
  322.                 {
  323.                     ScaleCache = *Buffer;
  324.  
  325.                         /* Print the character to be scaled into the
  326.                          * invisible drawing area.
  327.                          */
  328.  
  329.                     Move(ScaleRPort,0,Baseline);
  330.  
  331.                     Text(ScaleRPort,Buffer++,1);
  332.  
  333.                         /* Scale the font. */
  334.  
  335.                     BitMapScale(ScaleArgs);
  336.                 }
  337.                 else
  338.                     Buffer++;
  339.  
  340.                     /* Render the character. */
  341.  
  342.                 BltBitMapRastPort(ScaleDstBitMap,0,SrcY,RPort,WindowLeft + DestX,WindowTop + DestY,SizeX,TextFontHeight,MINTERM_COPY);
  343.             }
  344.             else
  345.             {
  346.                 ScaleCache = *Buffer;
  347.  
  348.                 if(Mode)
  349.                     SetFont(ScaleRPort,TextFont);
  350.                 else
  351.                     SetFont(ScaleRPort,GFX);
  352.  
  353.                 Mode ^= 1;
  354.  
  355.                     /* Print the character to be scaled into the
  356.                      * invisible drawing area.
  357.                      */
  358.  
  359.                 Move(ScaleRPort,0,Baseline);
  360.  
  361.                 Text(ScaleRPort,Buffer++,1);
  362.  
  363.                     /* Scale the font. */
  364.  
  365.                 BitMapScale(ScaleArgs);
  366.  
  367.                     /* Render the character. */
  368.  
  369.                 BltBitMapRastPort(ScaleDstBitMap,0,SrcY,RPort,WindowLeft + DestX,WindowTop + DestY,SizeX,TextFontHeight,MINTERM_COPY);
  370.             }
  371.  
  372.             DestX += SizeX;
  373.         }
  374.  
  375.         if(!Mode)
  376.             SetFont(ScaleRPort,GFX);
  377.     }
  378.     else
  379.     {
  380.             /* Run down the buffer... */
  381.  
  382.         while(Size--)
  383.         {
  384.             if(*Buffer != ScaleCache)
  385.             {
  386.                 ScaleCache = *Buffer;
  387.  
  388.                     /* Print the character to be scaled into the
  389.                      * invisible drawing area.
  390.                      */
  391.  
  392.                 Move(ScaleRPort,0,Baseline);
  393.  
  394.                 Text(ScaleRPort,Buffer++,1);
  395.  
  396.                     /* Scale the font. */
  397.  
  398.                 BitMapScale(ScaleArgs);
  399.             }
  400.             else
  401.                 Buffer++;
  402.  
  403.                 /* Render the character. */
  404.  
  405.             BltBitMapRastPort(ScaleDstBitMap,0,SrcY,RPort,WindowLeft + DestX,WindowTop + DestY,SizeX,TextFontHeight,MINTERM_COPY);
  406.  
  407.             DestX += SizeX;
  408.         }
  409.     }
  410. }
  411.